package cn.imvc.app.models;

import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Member;
import cn.imvc.app.entities.GoodsOrder;
import cn.imvc.app.repositories.GoodsOrderRepository;
import cn.imvc.data.domain.ViewRequest;

@Service
public class GoodsOrderModelImpl implements GoodsOrderModel {

	@Autowired
	private GoodsOrderRepository goodsOrderRepository;
	
	public GoodsOrder get(int id) {
		return goodsOrderRepository.findOne(id);
	}

	public Page<GoodsOrder> find(ViewRequest viewRequest) {
		return (Page<GoodsOrder>) goodsOrderRepository.findAll();
	}

	public GoodsOrder save(GoodsOrder goodsOrder) {
		GoodsOrder goodsOrder2 = goodsOrderRepository.saveAndFlush(goodsOrder);
		return goodsOrder2;
	}

	public GoodsOrder update(GoodsOrder newGoodsOrder) {
		GoodsOrder loadGoodsOrder = get(newGoodsOrder.getId());
		BeanUtils.copyProperties(newGoodsOrder, loadGoodsOrder, new String[] { "id" });
		return goodsOrderRepository.saveAndFlush(loadGoodsOrder);
	}

	public void delete(int id) {
		goodsOrderRepository.delete(id);
	}

	public void deleteBatch(int[] ids) {
		for (int id : ids) {
			delete(id);
		}
	}
	
	public GoodsOrder findByMemberAndId(Member member, Integer id){
		return goodsOrderRepository.findByMemberAndId(member, id);
	}
	
	public List<GoodsOrder> findByGoodsOrderIdIn(List<Integer> goodsOrderIds){
		return goodsOrderRepository.findByIdIn(goodsOrderIds);
	}
}
